home *** CD-ROM | disk | FTP | other *** search
- Path: mail2news.demon.co.uk!genesis.demon.co.uk
- From: Lawrence Kirby <fred@genesis.demon.co.uk>
- Newsgroups: comp.lang.c
- Subject: Re: HELP!
- Date: Thu, 14 Mar 96 20:02:17 GMT
- Organization: none
- Message-ID: <826833737snz@genesis.demon.co.uk>
- References: <4htsjm$v5o@lantana.singnet.com.sg> <3142BF08.1F5C@hsc.unt.edu>
- Reply-To: fred@genesis.demon.co.uk
- X-NNTP-Posting-Host: genesis.demon.co.uk
- X-Newsreader: Demon Internet Simple News v1.27
- X-Mail2News-Path: genesis.demon.co.uk
-
- In article <3142BF08.1F5C@hsc.unt.edu>
- sfogoros@hsc.unt.edu "Steve Fogoros" writes:
-
- >Teddy Bear wrote:
- >>
- >> Can anyone please help me my giving me a sample programme?
- >> I need to write a program that will enter and record an individual's
- >> details
- >> search (by name) and display an individual's details
- >> Delete an individual's details
- >> Save and restore the entire list to/from a disk file
- >>
- >> Emergency!
- >> Thanks
- >> my e-mail add is at s7700038@singnet.com.sg
- >
- >Here is a most basic sample program:
- >
- >#include "stdio.h"
- >#include "stdlib.h"
- >#include "string.h"
-
- These should be:
-
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
-
- >void enter(void);
- >void show(void);
- >void delete(void);
- >
- >FILE *fi, *fo;
- >
- >void main(void)
-
- main returns int in C.
-
- int main(void)
-
- >{
- > printf("1. enter a record\n");
- > printf("2. show a record\n");
- > printf("3. delete a record\n");
- > printf("4. exit\n");
- > printf("Press 1, 2, 3, or 4 then enter\n");
- >
- > switch((char)getc(stdin))
-
- getc(stdin) is more commonly called getchar()
-
- The (char) cast has no effect other than possibly to introduce implementation
- defined behaviour - you're better off without it. It is worth noting as
- a distantly related issue that character constants such as '1' have type int
- in C.
-
- > {
- > case '1': getc(stdin); enter(); break;
- > case '2': getc(stdin); show(); break;
- > case '3': getc(stdin); delete(); break;
- > case '4': exit(0); break;
- > default: printf("bad selection. program terminated\n"); exit(0); break;
- > }
-
- return 0;
-
- >}
- >
- >void enter(void)
- >{
- > char buff[80];
- >
- > printf("Enter data followed by return key\n");
- > fgets(buff,79,stdin);
-
- This is OK but you could also have written:
-
- fgets(buff,80,stdin);
-
- or better still
-
- fgets(buff,sizeof buff,stdin);
-
- the 2nd argument specifies the maximum number of characters that fgets()
- will write to the buffer including the terminating '\0'
-
- >
- > fi = fopen("dfile","a");
- > if(fi == 0)
-
- Tha's fine but you might consider demonstrating the pointer comparison
- explicitly with:
-
- if(fi == NULL)
-
- > {
- > printf("unable to open dfile. program terminated\n");
- > exit(0);
-
- Since this is a failure state you might also consider:
-
- exit(EXIT_FAILURE);
-
- EXIT_SUCCESS and EXIT_FAILURE are defined in stdlib.h. 0 return is an
- alternative to EXIT_SUCCESS for indicating success.
-
- > }
- >
- > fprintf(fi,"%s",buff);
- >
- > fclose(fi);
- >}
- >
- >void show(void)
- >{
- > char srch[80], buff[80];
- >
- > printf("Enter search string and enter\n");
- > fgets(srch,79,stdin);
-
- Same as before.
-
- > srch[strlen(srch)-1] = 0;
-
- This is dangerous - it is possible for fgets() to put a 0 length string in
- srch. It is also possible that the last character before '\0' is not '\n',
- not to mention the possibility of fgets() writing no characters to srch
- because of an end-of-file or error condition. Some of these thing can perhaps
- be ignored for keyboard input (but IMHO it is not a good idea) there
- is a simple trick that has recently been making the rounds of comp.lang.c
- which copes with the first two problems:
-
- strtok(srch, "\n");
-
- > printf("\n");
-
- There is also putchar('\n') which is the tool designed for this particular
- job. I know others prefer the uniformity of using printf everywhere.
-
- > fi = fopen("dfile","r");
- > if(fi == 0)
- > {
- > printf("unable to open dfile. program terminated\n");
- > exit(0);
- > }
- >
- > while(!feof(fi))
- > {
- > fgets(buff,79,fi);
- > if(feof(fi)) break;
- > if(strstr(buff,srch)) printf("%s",buff);
- > }
-
- It is quite rare that you need to use feof() needs to be used.
- Most standard library function have return values and they are there to
- be used. So:
-
- while(fgets(buff,sizeof buff,fi) != NULL)
- {
- if(strstr(buff,srch)) printf("%s",buff);
- }
-
- The loop test exits the loop on both an end-of-file and error condition
- If you wanted to find out wich occurred you could use ferror() or feof()
- here.
-
- > fclose(fi);
- >}
-
- ...
-
- --
- -----------------------------------------
- Lawrence Kirby | fred@genesis.demon.co.uk
- Wilts, England | 70734.126@compuserve.com
- -----------------------------------------
-